home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs1.zip / UUCICO / commlib.c < prev    next >
C/C++ Source or Header  |  1993-10-02  |  12KB  |  344 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       c o m m l i b . C                                            */
  3. /*                                                                    */
  4. /*       Generic communications library interface for UUPC/extended.  */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: commlib.c 1.9 1993/10/02 23:13:29 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: commlib.c $
  24.  * Revision 1.9  1993/10/02  23:13:29  ahd
  25.  * Allow suppressing TCPIP support
  26.  *
  27.  * Revision 1.8  1993/09/27  00:45:20  ahd
  28.  * Allow named pipes under OS/2 16 bit
  29.  *
  30.  * Revision 1.7  1993/09/24  03:43:27  ahd
  31.  * Add os/2 named pipes
  32.  *
  33.  * Revision 1.6  1993/09/20  04:46:34  ahd
  34.  * OS/2 2.x support (BC++ 1.0 support)
  35.  * TCP/IP support from Dave Watt
  36.  * 't' protocol support
  37.  *
  38.  * Revision 1.5  1993/07/22  23:22:27  ahd
  39.  * First pass at changes for Robert Denny's Windows 3.1 support
  40.  *
  41.  * Revision 1.4  1993/07/13  01:13:32  ahd
  42.  * Don't print NULL communications suite name!
  43.  *
  44.  * Revision 1.3  1993/07/11  14:38:32  ahd
  45.  * Display chosen suite
  46.  *
  47.  * Revision 1.2  1993/05/30  15:25:50  ahd
  48.  * Multiple driver support
  49.  *
  50.  * Revision 1.1  1993/05/30  00:01:47  ahd
  51.  * Initial revision
  52.  *
  53.  */
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                        System include files                        */
  57. /*--------------------------------------------------------------------*/
  58.  
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <time.h>
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                       UUPC/extended includes                       */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. #include "lib.h"
  68. #include "hlib.h"
  69. #include "commlib.h"
  70.  
  71. #include "ulib.h"             // Native communications interface
  72.  
  73. #if !defined(BIT32ENV) && !defined(FAMILYAPI) && !defined(_Windows)
  74. #include "ulibfs.h"           // DOS FOSSIL interface
  75. #include "ulib14.h"           // DOS ARTISOFT INT14 interface
  76. #endif
  77.  
  78. /*--------------------------------------------------------------------*/
  79. /*       Use the NOTCPIP to suppress the TCP/IP when you don't        */
  80. /*       have WINSOCK.H                                               */
  81. /*--------------------------------------------------------------------*/
  82.  
  83. #if defined(WIN32) || defined(_Windows)
  84. #ifndef NOTCPIP
  85. #include "ulibip.h"           // Windows sockets on TCP/IP interface
  86. #define TCPIP
  87. #endif
  88. #endif
  89.  
  90. #if defined(__OS2__) || defined(FAMILYAPI)
  91. #include "ulibnmp.h"          // OS/2 named pipes interface
  92. #endif
  93.  
  94. #define NATIVE "internal"
  95.  
  96. /*--------------------------------------------------------------------*/
  97. /*                          Global variables                          */
  98. /*--------------------------------------------------------------------*/
  99.  
  100. boolean portActive;         /* Port active flag for error handler   */
  101. boolean traceEnabled;        // Trace active flag
  102.  
  103. commrefi activeopenlinep, passiveopenlinep, swritep;
  104. commrefu sreadp;
  105. commrefv ssendbrkp, closelinep, SIOSpeedp, flowcontrolp, hangupp;
  106. commrefB GetSpeedp;
  107. commrefb WaitForNetConnectp;
  108. commrefb CDp;
  109.  
  110. /*--------------------------------------------------------------------*/
  111. /*                          Local variables                           */
  112. /*--------------------------------------------------------------------*/
  113.  
  114. static FILE *traceStream;    // Stream used for trace file
  115.  
  116. static short   traceMode;    // Flag for last data (input/output)
  117.                              // written to trace log
  118.  
  119. static boolean network = FALSE;  // Current communications suite is
  120.                                  // network oriented
  121.  
  122. currentfile();
  123.  
  124. /*--------------------------------------------------------------------*/
  125. /*       c h o o s e C o m m u n i c a t i o n s                      */
  126. /*                                                                    */
  127. /*       Choose communications suite to use                           */
  128. /*--------------------------------------------------------------------*/
  129.  
  130. boolean chooseCommunications( const char *name )
  131. {
  132.    static COMMSUITE suite[] =
  133.    {
  134.         { NATIVE,                      // Default for any opsys
  135.           nopenline, nopenline, nsread, nswrite,
  136.           nssendbrk, ncloseline, nSIOSpeed, nflowcontrol, nhangup,
  137.           nGetSpeed,
  138.           nCD,
  139.           (commrefb) NULL,
  140.           FALSE
  141.         },
  142. #if !defined(BIT32ENV) && !defined(_Windows) && !defined(FAMILYAPI)
  143.         { "fossil",                    // MS-DOS FOSSIL driver
  144.           fopenline, fopenline, fsread, fswrite,
  145.           fssendbrk, fcloseline, fSIOSpeed, fflowcontrol, fhangup,
  146.           fGetSpeed,
  147.           fCD,
  148.           (commrefb) NULL,
  149.           FALSE
  150.         },
  151.         { "articomm",                  // MS-DOS ARTISOFT INT14 driver
  152.           iopenline, iopenline, isread, iswrite,
  153.           issendbrk, icloseline, iSIOSpeed, iflowcontrol, ihangup,
  154.           iGetSpeed,
  155.           iCD,
  156.           (commrefb) NULL,
  157.           FALSE
  158.         },
  159. #endif
  160.  
  161. #if defined(TCPIP)
  162.         { "tcp/ip",                    // Win32 TCP/IP Winsock interface
  163.           tactiveopenline, tpassiveopenline, tsread, tswrite,
  164.           tssendbrk, tcloseline, tSIOSpeed, tflowcontrol, thangup,
  165.           tGetSpeed,
  166.           tCD,
  167.           tWaitForNetConnect,
  168.           TRUE
  169.         },
  170. #endif
  171.  
  172. #if defined(__OS2__) || defined(FAMILYAPI)
  173.         { "namedpipes",                // OS/2 named pipes
  174.           pactiveopenline, ppassiveopenline, psread, pswrite,
  175.           pssendbrk, pcloseline, pSIOSpeed, pflowcontrol, phangup,
  176.           pGetSpeed,
  177.           pCD,
  178.           pWaitForNetConnect,
  179.           TRUE
  180.         },
  181. #endif
  182.         { NULL }                       // End of list
  183.    };
  184.  
  185.    int subscript = 0;
  186.  
  187. /*--------------------------------------------------------------------*/
  188. /*                   Search for name in suite table                   */
  189. /*--------------------------------------------------------------------*/
  190.  
  191.    while (( name  != NULL ) && (suite[subscript].type != NULL ))
  192.    {
  193.       if ( equali(name,suite[subscript].type))
  194.          break;                           // Success!
  195.       else
  196.          subscript++;
  197.    } /* while */
  198.  
  199.    if ( suite[subscript].type == NULL )
  200.    {
  201.       printmsg(0,"chooseCommunications: Invalid suite name %s",
  202.                   name );
  203.       return FALSE;
  204.    }
  205.  
  206. /*--------------------------------------------------------------------*/
  207. /*       We have a valid suite, define the routines to use and        */
  208. /*       return to caller                                             */
  209. /*--------------------------------------------------------------------*/
  210.  
  211.    activeopenlinep    = suite[subscript].activeopenline;
  212.    passiveopenlinep   = suite[subscript].passiveopenline;
  213.    sreadp             = suite[subscript].sread;
  214.    swritep            = suite[subscript].swrite;
  215.    ssendbrkp          = suite[subscript].ssendbrk;
  216.    closelinep         = suite[subscript].closeline;
  217.    SIOSpeedp          = suite[subscript].SIOSpeed;
  218.    flowcontrolp       = suite[subscript].flowcontrol;
  219.    hangupp            = suite[subscript].hangup;
  220.    GetSpeedp          = suite[subscript].GetSpeed;
  221.    CDp                = suite[subscript].CD;
  222.    WaitForNetConnectp = suite[subscript].WaitForNetConnect;
  223.    network            = suite[subscript].network;
  224.  
  225.    printmsg(equal(suite[subscript].type, NATIVE) ? 5 : 4,
  226.             "chooseCommunications: Chose suite %s",
  227.             suite[subscript].type );
  228.  
  229.    return TRUE;
  230.  
  231. } /* chooseCommunications */
  232.  
  233. /*--------------------------------------------------------------------*/
  234. /*       t r a c e S t a r t                                          */
  235. /*                                                                    */
  236. /*       Begin communicatons line tracing                             */
  237. /*--------------------------------------------------------------------*/
  238.  
  239. boolean traceStart( const char *port )
  240. {
  241.    char *linelog;
  242.    time_t now;
  243.  
  244.    if ( ! traceEnabled )
  245.       return FALSE;
  246.  
  247.    linelog = normalize( "LineData.Log" );
  248.  
  249.    if ( traceStream != NULL )
  250.    {
  251.       printmsg(0,"traceOn: Trace file %s already open!", linelog);
  252.       panic();
  253.    }
  254.  
  255.    traceStream = FOPEN( linelog ,"a", BINARY_MODE);
  256.  
  257.    if ( traceStream == NULL )
  258.    {
  259.       printerr( linelog );
  260.       printmsg(0, "Unable to open trace file, tracing disabled");
  261.       traceEnabled = FALSE;
  262.       return FALSE;
  263.    }
  264.  
  265.    time( &now );
  266.  
  267.    fprintf(traceStream,"Trace begins for port %s at %s",
  268.            port, ctime( &now ));
  269.  
  270.    printmsg(4,"Tracing communications port %s in file %s",
  271.             port, linelog );
  272.  
  273.    traceMode  = 2;               // Make sure first trace includes
  274.                                  // prefix with direction
  275.  
  276.    return TRUE;                  // Success to caller
  277.  
  278. } /* traceStart */
  279.  
  280. /*--------------------------------------------------------------------*/
  281. /*       t r a c e S t o p                                            */
  282. /*                                                                    */
  283. /*       Terminate communications line tracing                        */
  284. /*--------------------------------------------------------------------*/
  285.  
  286. void traceStop( void )
  287. {
  288.    if ( traceStream != NULL )
  289.    {
  290.       time_t now = time( NULL );
  291.       fprintf(traceStream,"\nTrace complete at %s",  ctime( &now ));
  292.       fclose( traceStream );
  293.       traceStream = NULL;
  294.    }
  295.  
  296. } /* traceStop */
  297.  
  298. /*--------------------------------------------------------------------*/
  299. /*       t r a c e D a t a                                            */
  300. /*                                                                    */
  301. /*       Write traced data to the log                                 */
  302. /*--------------------------------------------------------------------*/
  303.  
  304. void traceData( const char *data,
  305.                 const short len,
  306.                 const boolean output)
  307. {
  308. #ifdef VERBOSE
  309.    int subscript;
  310. #endif
  311.  
  312.    if ( ! traceEnabled || ! len )
  313.       return;
  314.  
  315.    if ( traceMode != (short) output )
  316.    {
  317.       fputs(output ? "\nWrite: " : "\nRead:  ",traceStream );
  318.       traceMode = (short) output;
  319.    }
  320.  
  321. #ifdef VERBOSE
  322.    for (subscript = 0; subscript < len; subscript++)
  323.    {
  324.       fprintf( traceStream, "%2.2x", data[subscript] );
  325.    } /* for */
  326. #else
  327.  
  328.    fwrite(data, 1, len, traceStream ); // Write out raw data
  329.  
  330. #endif
  331.  
  332. } /* traceData */
  333.  
  334. /*--------------------------------------------------------------------*/
  335. /*       I s N e t w o r k                                            */
  336. /*                                                                    */
  337. /*       Report if current communications suite is network oriented   */
  338. /*--------------------------------------------------------------------*/
  339.  
  340. boolean IsNetwork(void)
  341. {
  342.    return network;         // Preset when suite initialized
  343. }
  344.